package com.fb4_messenger;





import java.util.List;

import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.view.MenuItem;
import android.widget.EditText;
//import android.widget.EditText;
import android.widget.ListView;
import android.support.v4.app.NavUtils;

public class DisplayMessageActivity extends Activity {

	
	private com.fb4_messenger.ConversationArrayAdapter adapter;
	private ListView lv;
	private EditText editText1;
	
	
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_discuss);
		    
        Intent intent = getIntent();
        //String userName = intent.getStringExtra(MainActivity.userName);
        
        fetchObjectsFromParse();
		
        lv = (ListView) findViewById(R.id.listView1);

		adapter = new ConversationArrayAdapter(getApplicationContext(), R.layout.activity_display_message);

		editText1 = (EditText) findViewById(R.id.editText1);
		lv.setAdapter(adapter);

		//editText1 = (EditText) findViewById(R.id.editText1);
		
	}

	public void fetchObjectsFromParse() {
		ParseQuery<ParseObject> query = ParseQuery.getQuery("Message");
		query.setCachePolicy(ParseQuery.CachePolicy.NETWORK_ELSE_CACHE);
//		query.whereEqualTo("sender", "Christoph");
//
		query.findInBackground(new FindCallback<ParseObject>() {
		    public void done(List<ParseObject> messageList, ParseException e) {
		        if (e == null) {
		        	
		        	for(ParseObject po : messageList) {
		        		if(po.getString("sender").equalsIgnoreCase("Christoph")) 
		        			adapter.add(new Message(false,po.getString("message")));
		        		else
		        			adapter.add(new Message(true,po.getString("message")));
		        	}
		        	
		            
		        } else {
		            
		        }
		    }


		});		
	}


	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}
